import { API } from "@/api/modules/dwh/typings";
import { ColumnProps } from "@/components/ProTable/interface";
import { DatePickerFormat, DimensionExtraEnum, DimensionTypeEnum, SymbolEnum } from "@/utils/dict";
import { reactive, ref, toValue, watchEffect } from "vue";
import SelectParamForm from "@/views/dwh/tableData/components/selectParamForm.vue";
import dayjs, { ManipulateType } from "dayjs";
import SelectSearchFormItem from "@/components/SearchForm/components/SelectSearchFormItem.vue";

export const usePackDimension = (tableColumns: any, enumMap: any) => {
  const validateRefs = ref([]);
  const addValidateRef = (el: any) => {
    if (el) {
      validateRefs.value.push(el);
    }
  };

  const primaryKeyCode = ref("");

  let columnList = ref<API.TableColumnVo[]>([]);
  const searchParam: {
    [key: string]: API.WhereParam;
  } = reactive({});

  const aggColumn = ref<API.TableColumnVo[]>([]);

  function packDimension(tableColumn: any, column: ColumnProps<API.TableColumnVo>, addValidateRef: Function) {
    if (tableColumn.columnType == "date") {
      column.search = {
        render: () => {
          return (
            <el-date-picker
              v-model={searchParam[tableColumn.columnCode].value}
              type={tableColumn.dimensionExtra}
              unlink-panels
              value-format={DatePickerFormat[tableColumn.dimensionExtra!]}
              start-placeholder="开始日期"
              range-separator="-"
              end-placeholder="结束日期"
            />
          );
        }
      };
      return;
    }
    if (getOptions(tableColumn)) {
      column.search = {
        render: () => {
          return (
            <SelectSearchFormItem
              column={column}
              v-model:value={searchParam[tableColumn.columnCode].value}
              v-model:symbol={searchParam[tableColumn.columnCode].symbol}
            />
          );
        }
      };
      return;
    }
    column.search = {
      render: () => {
        return (
          <SelectParamForm
            ref={el => addValidateRef(el)}
            v-model:value={searchParam[tableColumn.columnCode].value}
            v-model:symbol={searchParam[tableColumn.columnCode].symbol}
          ></SelectParamForm>
        );
      }
    };
  }

  // 是否有下拉选项
  const getOptions = (tableColumn: API.TableColumnVo) => {
    // if (tableColumn.columnOptions?.length > 0) {
    //   return tableColumn.columnOptions;
    // }
    // return [];
    return tableColumn.columnOptions;
  };

  function packSearchParam(tableColumn: API.TableColumnVo) {
    searchParam[tableColumn.columnCode] = {
      columnCode: tableColumn.columnCode,
      symbol: SymbolEnum.EQ,
      value: tableColumn.defaultValue ?? ""
    };

    if (tableColumn.columnType == "date") {
      if (tableColumn.defaultValue) {
        let values = tableColumn.defaultValue.split("_");
        let startDate = dayjs()
          .subtract(Number(values[0]), values[1] as ManipulateType)
          .format(DatePickerFormat[tableColumn.dimensionExtra]);

        if (tableColumn.dimensionExtra!.includes("range")) {
          searchParam[tableColumn.columnCode].value = [startDate, dayjs().format(DatePickerFormat[tableColumn.dimensionExtra])];
        } else {
          searchParam[tableColumn.columnCode].value = startDate;
        }
      }
    }
  }

  const packTableColumn = (tableColumns: API.TableColumnVo[], enumMap: any) => {
    // 清空hook属性
    columnList.value.length = 0;
    Object.assign(searchParam, {});
    aggColumn.value.length = 0;
    validateRefs.value.length = 0;
    columnList.value.push({ prop: "operation", label: "操作", fixed: "right", width: 225 });

    primaryKeyCode.value =
      tableColumns.find(column => column.dimensionExtra === DimensionExtraEnum.dimension_primary_key)?.columnCode ?? "";

    for (const tableColumn of tableColumns) {
      if (tableColumn.dimensionType == "agg") {
        aggColumn.value.push(tableColumn);
        continue;
      }
      let column: ColumnProps<API.TableColumnVo> = {
        prop: tableColumn.columnCode,
        label: tableColumn.columnName,
        srcColumn: tableColumn,
        isShow: tableColumn.show == undefined ? true : tableColumn.show == 1
      };
      if (tableColumn.dimensionType == DimensionTypeEnum.dimension) {
        packSearchParam(tableColumn);

        packDimension(tableColumn, column, addValidateRef);
      }
      columnList.value.push(column);
    }
  };
  watchEffect(() => {
    if (toValue(tableColumns)) {
      packTableColumn(toValue(tableColumns), toValue(enumMap));
    }
  });

  const searchInitParam = reactive({});
  Object.assign(searchInitParam, searchParam);
  return { columnList, searchParam, aggColumn, validateRefs, searchInitParam, primaryKeyCode };
};
